<template>
  <div class="login-container">
    <div class="login-card">
      <!-- 忘记密码标题 -->
      <h2 class="login-title">忘记密码</h2>
      
      <!-- 提示信息 -->
      <div class="password-hint">
        请输入您的邮箱地址，我们将发送密码重置链接至您的邮箱
      </div>

      <!-- 表单 -->
      <el-form label-position="top" class="login-form">
        <!-- Email 输入 -->
        <el-form-item label="邮箱">
          <el-input 
            placeholder="请输入注册时使用的邮箱地址" 
            size="large"
            class="custom-input"
            v-model="email"
          />
        </el-form-item>
        
        <!-- 发送按钮 -->
        <el-button 
          type="primary" 
          size="large" 
          class="login-btn"
          @click="handleRecoverPassword"
          :loading="loading"
        >
          发送重置邮件
        </el-button>
        
        <!-- 返回登录链接 -->
        <div class="register-link">
          想起密码?
          <el-link type="primary" @click="goToLogin">返回登录</el-link>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { userApi } from '@/utils/api'

export default {
  data() {
    return {
      email: "1962155350@qq.com",
      loading: false
    };
  },
  methods: {
    goToLogin() {
      // 这里添加导航到登录页的逻辑
      this.$router.push('/login');
    },
    async handleRecoverPassword() {
      if (!this.email) {
        this.$message.error('请输入邮箱地址');
        return;
      }

      // 简单的邮箱格式验证
      const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
      if (!emailRegex.test(this.email)) {
        this.$message.error('请输入正确的邮箱格式');
        return;
      }

      this.loading = true;
      try {
        await userApi.recoverPassword(this.email);
        this.$message.success('密码重置邮件已发送，请检查您的邮箱');
      } catch (error) {
        console.error('发送重置邮件失败:', error);
        let errorMessage = '发送重置邮件失败，请稍后重试';
        
        if (error.response) {
          switch (error.response.status) {
            case 404:
              errorMessage = '该邮箱未注册';
              break;
            case 422:
              errorMessage = '邮箱格式不正确';
              break;
            case 500:
              errorMessage = '服务器内部错误，请稍后重试';
              break;
            default:
              errorMessage = error.response.data?.detail || errorMessage;
          }
        } else if (error.request) {
          errorMessage = '网络连接失败，请检查网络设置';
        }
        
        this.$message.error(errorMessage);
      } finally {
        this.loading = false;
      }
    }
  }
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #fff;
  padding: 20px;
}

.login-card {
  width: 100%;
  max-width: 420px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
  padding: 40px;
}

.login-title {
  text-align: center;
  font-size: 24px;
  font-weight: 600;
  color: #333;
  margin-bottom: 20px;
}

.password-hint {
  text-align: center;
  color: #666;
  font-size: 14px;
  margin-bottom: 30px;
  line-height: 1.5;
  padding: 0 10px;
}

.login-form {
  margin-bottom: 20px;
}

.login-btn {
  width: 100%;
  height: 44px;
  font-size: 16px;
  margin-top: 10px;
  font-weight: 500;
}

.register-link {
  display: flex; 
  align-items: center; 
  justify-content: center;
  gap: 8px;
  margin-top: 20px;
  font-size: 14px;
  color: #606266;
}

/* 输入框高度和样式调整 */
.custom-input :deep(.el-input__inner) {
  height: 40px !important;
  line-height: 40px;
  padding: 0 15px;
  font-size: 16px;
}

/* 表单项垂直间距调整 */
:deep(.el-form-item) {
  margin-bottom: 24px;
}

/* 表单标签样式调整 */
:deep(.el-form-item__label) {
  font-weight: 500;
  color: #606266;
  padding-bottom: 8px;
  font-size: 14px;
  display: block;
  width: 100%;
}

/* 整体表单对齐优化 */
.login-form {
  display: flex;
  flex-direction: column;
}
</style>